package com.sunny.webflux.config;

import com.sunny.webflux.handler.TimeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * @author sunny
 * @date 2018/11/30
 */
@Configuration
public class RouterConfig {

    @Autowired
    private TimeHandler timeHandler;

    /**
     * webflux方式请求
     *
     * @author sunny
     * @date 2018/11/30
     * @return org.springframework.web.reactive.function.server.RouterFunction<org.springframework.web.reactive.function.server.ServerResponse>
     */
    @Bean
    public RouterFunction<ServerResponse> timeRouter() {
        return route(GET("/date"), timeHandler::getDate)
                .andRoute(GET("/dateTime"), timeHandler::getDateTime)
                .andRoute(GET("/times"), timeHandler::sendTimePerSec);
    }
}
